package com.softer.wxzj.controller;


import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.aop.annotation.LogRecord;
import com.softer.wxzj.common.aop.enums.LogOperationType;
import com.softer.wxzj.common.aop.enums.LogOperationUnit;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.entity.*;
import com.softer.wxzj.service.BaseOwnerInfoService;
import com.softer.wxzj.service.SysOperatorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Niu
 * @create 2019/5/21 - 9:14 - 星期二
 */

@RestController
@RequestMapping("/baseOwnerInfo")
@Slf4j
public class BaseOwnerInfoController {

    @Autowired
    BaseOwnerInfoService baseOwnerInfoService;

    @Autowired
    SysOperatorService sysOperatorService;

    /**
     * 新增业主
     *
     * @param baseOwnerInfoList 业主/共有人集合
     * @return 结果
     */
    @PostMapping("/add")
    @LogRecord(description = "新增业主", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.INSERT)
    public CommonResult add(@RequestBody List<BaseOwnerInfo> baseOwnerInfoList) {
        return baseOwnerInfoService.insert(baseOwnerInfoList);
    }

    /**
     * 修改业主
     * @param baseOwnerInfoList 业主/共有人集合
     * @return 结果
     */
    @PostMapping("/update")
    @LogRecord(description = "修改业主", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult updateOwnerAndHouse(@RequestBody List<BaseOwnerInfo> baseOwnerInfoList) {
        return baseOwnerInfoService.updateOwnerAndHouse(baseOwnerInfoList);
    }

    /**
     * 根据房间ID查询业主和共有人
     *
     * @param houseId 房间Id
     * @param arcType 图档类型
     * @return 结果
     */
    @PostMapping("/getOwnerListByHouseId")
    @LogRecord(description = "根据房间ID查询业主和共有人", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.SELECT)
    public List<BaseOwnerInfoMap> getOwnerListByHouseId(String houseId, String arcType) {
        return baseOwnerInfoService.getOwnerListByHouseId(houseId, arcType);
    }

    /**
     * 新增业主前，查询业主房间的信息
     *
     * @param houseId 房间Id
     * @return 结果
     */
    @PostMapping("/getOwnerHouseInfo")
    @LogRecord(description = "新增业主前，查询业主房间的信息", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.SELECT)
    public BaseOwnerInfoMap getOwnerHouseInfo(String houseId) {
        return baseOwnerInfoService.getOwnerHouseInfo(houseId);
    }

    /**
     * 获取业主信息列表
     * 查询当前登录账户管理下的小区业主（非保存状态和删除状态）的信息列表
     *
     * @return
     */
    @RequestMapping("/getBaseOwnerInfoList")
    @LogRecord(description = "获取业主信息列表", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseOwnerInfoList> getBaseOwnerInfoList(Page page, BaseOwnerInfoList baseOwnerInfo) {
        return baseOwnerInfoService.getBaseOwnerInfoList(page, baseOwnerInfo);
    }

    /**
     * 根据业主id查询业主基本信息
     * @param id 业主id  （uuid）
     * @return
     */
    @RequestMapping("/getBaseOwnerInfoModule")
    @LogRecord(description = "根据业主id查询业主基本信息", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.SELECT)
    public BaseOwnerInfo getBaseOwnerInfoModule(@RequestParam("id")String id) {
        log.info("查询业主id:{}查询业主基本信息", id);
        return baseOwnerInfoService.getBaseOwnerInfoModule(id);
    }

    /**
     * @param page          分页
     * @param baseOwnerInfo 业主信息实体
     * @return 数据
     */
    @RequestMapping("/getList")
    @LogRecord(description = "获取房屋业主信息变更列表", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseOwnerInfoList> getList(Page page, BaseOwnerInfoList baseOwnerInfo) {
        return baseOwnerInfoService.getList(page, baseOwnerInfo);
    }

    /**
     * 模糊查询业主信息列表
     *
     * @param page  分页实体
     * @param param 模糊查询参数
     * @return 业主信息
     */
    @RequestMapping("/fuzzySearch")
    @LogRecord(description = "模糊查询业主信息列表", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseOwnerFuzzy> fuzzySearch(Page page, String param) {
        return baseOwnerInfoService.fuzzySearch(page, param);
    }

    /**
     * 查询房屋业主信息详情
     *
     * @param id 业主ID
     * @return 业主信息实体
     */
    @RequestMapping("/getInfo")
    @LogRecord(description = "查询单条数据", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.SELECT)
    public BaseOwnerInfoMap getInfo(String id) {
        return baseOwnerInfoService.getInfoMap(id);
    }

    /**
     * 房屋业主信息变更信息: 新增一条变更记录以及增加变更详情
     *
     * @param baseOwnerInfo 业主信息实体
     * @return
     */
    @RequestMapping("/updateToSave")
    @LogRecord(description = "房屋业主信息变更信息", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult updateToSave(@RequestBody BaseOwnerInfoUpdate baseOwnerInfo) throws Exception {
        return baseOwnerInfoService.updateToSave(baseOwnerInfo.getBaseOwnerInfo(), baseOwnerInfo.getModifyType(), baseOwnerInfo.getModifyCause());
    }

    /**
     * 变更审核
     *
     * @param id      变更Id
     * @param opinion 审核意见
     * @param mark    审批通过 audit_pass
     *                审批否决 audit_veto
     *                审批退回 audit_back
     * @return 审核结果
     */
    @RequestMapping("/modifyAudit")
    public CommonResult modifyAudit(@RequestParam("id") String id, String opinion, @RequestParam("mark") String mark) {
        return baseOwnerInfoService.modifyAudit(id, opinion, mark);
    }

    /**
     * 根据id查询变更详情
     *
     * @param id 变更类型id
     * @return
     */
    @RequestMapping("/getModifyDetailList")
    @LogRecord(description = "根据id查询变更详情", operationUnit = LogOperationUnit.BASE_ORGANIZATION, operationType = LogOperationType.SELECT)
    public PageInfo<BaseModifyDetail> getModifyDetailList(Page page, String id) {
        return baseOwnerInfoService.getModifyDetailList(page, id);
    }

    /**
     * 新增或修改批量操作
     *
     * @param baseOwnerInfoList
     * @return
     */
    @RequestMapping("/insertOrUpdate")
    @LogRecord(description = "房屋业主新增或修改", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.INSERT_OR_UPDATE)
    public CommonResult insertOrUpdate(@RequestBody List<BaseOwnerInfo> baseOwnerInfoList) {
        return baseOwnerInfoService.insertOrUpdate(baseOwnerInfoList);
    }

    @RequestMapping("/delete")
    @LogRecord(description = "房屋业主删除", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.DELETE)
    public CommonResult delete(@RequestParam("id") String id) {
        return baseOwnerInfoService.delete(id);
    }

    @PostMapping("/bindSubHouse")
    @LogRecord(description = "绑定附房", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult bindSubHouse(@RequestBody List<BaseOwnerInfo> baseOwnerInfoList, @RequestParam("houseId") String houseId) {
        return baseOwnerInfoService.bindSubHouse(baseOwnerInfoList, houseId, "");
    }

    @PostMapping("/getSubHouseList")
    @LogRecord(description = "查询附房列表", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.SELECT)
    public List<BaseOwnerInfo> getSubHouseList(@RequestParam("accNo") String accNo) {
        return baseOwnerInfoService.getSubHouseList(accNo);
    }

    /**
     * 关联附房 非公用方法
     *
     * @param baseOwnerInfoList
     * @return
     */
    @PostMapping("/insertOrUpdateToView")
    @LogRecord(description = "关联附房", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult insertOrUpdateToView(@RequestBody List<BaseOwnerInfo> baseOwnerInfoList) {
        return baseOwnerInfoService.insertOrUpdateToView(baseOwnerInfoList);
    }

    @PostMapping("/deleteToView")
    @LogRecord(description = "批量删除业主，用在视图批量维护业主界面", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult deleteToView(String idList) {
        return baseOwnerInfoService.deleteToView(idList);
    }

    /**
     * 根据房屋id查询业主信息
     */
    @RequestMapping("/selectOwner")
    @LogRecord(description = "根据房屋id查询业主信息", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.UPDATE)
    public BaseOwnerInfo selectOwner(String houseId) {
        return baseOwnerInfoService.selectOwner(houseId);
    }

    @RequestMapping("/selectOwnerByOwnerNo")
    @LogRecord(description = "旧业主信息查询", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.UPDATE)
    public List<BaseOwnerInfo> selectOwnerByOwnerNo(String ownerNo) {
        return baseOwnerInfoService.selectOwnerByOwnerNo(ownerNo);
    }

    /**
     * 查询新业主信息
     *
     * @param qId
     * @return
     */
    @RequestMapping("/selectNewowner")
    @LogRecord(description = "新业主信息查询", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.UPDATE)
    public List<BaseOwnerInfo> selectNewowner(String qId) {
        return baseOwnerInfoService.selectNewowner(qId);
    }

    /**
     * @param page          分页
     * @param baseOwnerInfo 业主信息实体
     * @return 数据
     */
    @RequestMapping("/getListChange")
    @LogRecord(description = "获取房屋业主信息列表", operationUnit = LogOperationUnit.BASE_OWNER_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseOwnerInfoList> getListChange(Page page, BaseOwnerInfoList baseOwnerInfo) {
        return baseOwnerInfoService.getList(page, baseOwnerInfo);
    }
}
